<!-- 修改货到管理商品的页面 -->
<template>
	<view class="cargoWayEdit">
		<!-- 第一个 -->
		<view>
			<view>
				<text>货道</text>
				<view>
					<text>{{info.channel_no}}</text>
				</view>
			</view>
			<view>
				<text>商品</text>
				<view @click="fclock">
					<text>{{info.goods_title || '请选择商品'}}</text>
					<u-icon name="arrow-right" size="20rpx"></u-icon>
				</view>
			</view>
			<view>
				<text>货道状态</text>
				<view>
					<!-- 开关组件 -->
					<u-switch v-model="info.channel_status" size="30" text="开启" closeText="关闭" :active-value="1"
						:inactive-value="0" active-color="#FCB527"></u-switch>
				</view>
			</view>
			<view>
				<text>货道测试</text>
				<view>
					<text @click="ftest">开始测试</text>
					<u-icon name="arrow-right" size="20rpx"></u-icon>
				</view>
			</view>
			<view>
				<text>货道排序</text>
				<view>
					<text>{{info.weight}}</text>
					<!-- <u-icon name="arrow-right" size="20rpx"></u-icon> -->
				</view>
			</view>
			<view>
				<text>上架状态</text>
				<view>
					<!-- 开关组件 -->
					<u-switch v-model="info.status" size="30" text="上架" closeText="下架" :active-value="1"
						:inactive-value="0" active-color="#FCB527"></u-switch>
				</view>
			</view>

		</view>

		<!-- 第二个 -->
		<view>
			<view>
				<text>最大库存</text>
				<view>
					<text>{{info.max_stock}}</text>
				</view>
			</view>
			<view>
				<text>当前库存</text>
				<view>
					<u-input input-align="right" :border="false" v-model="info.stock"
						placeholder="请输入"></u-input>
				</view>
			</view>
			<view>
				<text>商品价格</text>
				<view>
					<text>{{info.sales_price}}</text>
				</view>
			</view>
		</view>

		<ButtonConfirm @click="save" myTxt="保存"></ButtonConfirm>


		<!-- 弹出层的东西 -->
		<view class="cargoWay_pop">
			<!-- 对话框 -->
			<u-modal :closeOnClickOverlay="true" :showCancelButton="true" :buttonReverse="true" confirmText="是"
				cancelText="否" confirmColor="#FCB527" cancelColor="#FCB527" :show="show" :title="title"
				:content='content' @cancel="fcancel" @confirm="fconfirm" @close="show = false"></u-modal>
		</view>
	</view>
</template>

<script>
	import ButtonConfirm from '@/components/buttonConfirm.vue'
	import home from '@/api/home.js';
	export default {
		components: {
			ButtonConfirm
		},
		data() {
			return {
				value: true, // 开关值
				// 弹出框
				show: false,
				title: '提示',
				content: '确定2号货道进行旋转测试吗？',
				info: {},
				newInfo: {}
			};
		},
		onLoad(op) {
			this.getDeviceChannalInfo(op)
		},
		onShow() {
			if(!this.$store.state.selectedList.id) return
			this.info.goods_id = this.$store.state.selectedList.id
			this.info.goods_title = this.$store.state.selectedList.goods_title
		},
		methods: {
			isObjectValueEqualNew(obj1, obj2) {
				let that = this
				// 判断两个变量是否为对象类型
				let isObj = (toString.call(obj1) === '[object Object]' && toString.call(obj2) === '[object Object]');
				if (!isObj) {
					return false;
				}
				// 判断两个对象的长度是否相等，不相等则直接返回 fase
				let obj1Keys = Object.keys(obj1);
				let obj2Keys = Object.keys(obj2);
				if (Object.keys(obj1).length !== Object.keys(obj2).length) {
					return false;
				}
				// 判断两个对象的每个属性值是否相等
				for (const key in obj1) {
					// 判断两个对象的键是否相等
					if (obj2.hasOwnProperty.call(obj2, key)) {
						let obj1Type = toString.call(obj1[key]);
						let obj2Type = toString.call(obj2[key]);
						// 如果值是对象，则递归
						if (obj1Type === '[object Object]' || obj2Type === '[object Object]') {
							if (!that.isObjectValueEqualNew(obj1[key], obj2[key])) {
								return false;
							}
						} else if (obj1[key] !== obj2[key]) {
							return false; // 如果不是对象，则判断值是否相等
						}
					} else {
						return false;
					}
				}
				return true; // 上面条件都通过，则返回 true
			},
			save(){
				if (this.isObjectValueEqualNew(this.info, this.newInfo)) {
					uni.showToast({
						title: "暂无更新数据",
						icon: 'none'
					})
					return
				}
				this.editDeviceChannal()
			},
			async getDeviceChannalInfo(op) {
				const res = await home.getDeviceChannalInfo(op)
				this.info = res.data
				this.newInfo = JSON.parse(JSON.stringify(res.data))
			},
			async editDeviceChannal() {
				const res = await home.editDeviceChannal({
					device_no: this.info.device_no,
					channel_no: this.info.channel_no,
					stock: this.info.stock,
					status: this.info.status,
					goods_id: this.info.goods_id,
					channel_status: this.info.channel_status
				})
				if(res.code == 200) {
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					this.getDeviceChannalInfo({
						device_no: this.info.device_no,
						channel_no: this.info.channel_no
					})
				}
				
			},
			fclock() {
				uni.navigateTo({
					url: '/pages/equipment/productManage'
				});
			},

			// 开始测试
			ftest() {
				this.show = true
			},

			// 确定
			fconfirm() {
				this.show = false
				console.log('点击确定');
			},

			// 取消
			fcancel() {
				this.show = false
				console.log('点击取消');
			}
		}
	}
</script>
<style>
	page{
		height: 100%;
		background: #F2F2F2;
	}
</style>
<style lang="scss" scoped>
	/deep/.u-input {
		padding: 0 !important;
	}
	
	.cargoWayEdit {
		height: 100%;
		padding-top: 20rpx;
		&>view:nth-child(1),
		&>view:nth-child(2) {
			box-sizing: border-box;
			padding: 0rpx 34rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 16rpx 3rpx rgba(216, 216, 216, 0.5);
			

			// 每一行
			&>view {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 88rpx;
				box-sizing: border-box;
				padding: 0 6rpx;

				&:not(:last-child) {
					border-bottom: 1px solid #D8D8D8;
				}

				&>text {
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
				}

				&>view {
					display: flex;
					align-items: center;

					&>image {
						width: 12rpx;
						height: 20rpx;
						margin-left: 10rpx;
					}

					&>text {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}

			}
		}


		// 第二个
		&>view:nth-child(2) {
			margin-bottom: 80rpx;
		}
	}
</style>